<template>
  <el-form label-width="100px" label-position="left">
    <el-form-item label="图层名称" prop="layerName">
      <el-input
        v-model.trim="formData.layerName"
        size="default"
        clearable
        style="width: 100%"
        @change="changed($event, 'layerName')"
      />
    </el-form-item>
    <el-form-item label="图片" prop="imageUrl">
      <UploadImg
        v-model="formData.imageUrl"
        height="88px"
        width="auto"
        class="min-w-80px"
        @update:model-value="(val) => changed(val, 'imageUrl')"
      />
    </el-form-item>
    <el-form-item label="图片地址" prop="jumpUrl">
      <el-input
        v-model.trim="formData.jumpUrl"
        size="default"
        clearable
        style="width: 100%"
        @change="changed($event, 'jumpUrl')"
      />
    </el-form-item>
    <el-form-item label="跳转方式" prop="jumpMode">
      <el-radio-group v-model="formData.jumpMode" @change="(val) => changed(val, 'jumpMode')">
        <el-radio v-for="item in jumpModeOptions" :key="item.code" :value="item.code">{{
          item.name
        }}</el-radio>
      </el-radio-group>
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const formData = reactive<any>({})

const jumpModeOptions = ref([
  {
    code: 'self',
    name: '本窗口'
  },
  {
    code: 'other',
    name: '新窗口'
  }
])

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>
<style lang="scss" scoped></style>
